import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yangxianming on 2015-11-02.
 */
public class ChineseFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String t = sdf1.format(new Date());

        String uri = request.getRequestURI();
        System.out.println(t + " uri: " + uri);
        //阻止直接访问敏感数据
        if (uri.contains("data/uploadQRcode")){
            System.out.println("bad uri" + uri);
            //PrintWriter写在这里可以确保数据流使用后关闭
            PrintWriter out = response.getWriter();
            out.write("Illegal access");
        }else {
//            System.out.println("good uri");
            servletRequest.setCharacterEncoding("UTF-8");
            filterChain.doFilter(servletRequest, servletResponse);
            servletResponse.setCharacterEncoding("UTF-8");
        }
    }

    @Override
    public void destroy() {

    }
}
